alert("MainScene.js loaded");

function SceneMainScene(options) {
    this.recommender = new Recommender;
    this.recommendations = new Array();
    this.currentGenreIndex = 0;
    this.currentProgramIndex = 0;
	this.options = options;
}

SceneMainScene.prototype.initialize = function () {
	alert("SceneMainScene.initialize()");
    var genreButtonText = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    
    genreButtonText += genreButtonText + genreButtonText + genreButtonText + genreButtonText + genreButtonText;

    this.recommender.sendRequests();
    this.recommendations = this.recommender.getRecommendations();

    $('#genreButton').sfButton({text:'<' + genreButtonText + '>'});
    $('#genreLabel').sfLabel({text:this.recommendations.genres[0], width:'150px'});
    $('#recommendations').sfList({data:this.recommendations.programs[0], index:'0', itemsPerPage: (this.recommendations.length < 5 ? this.recommendations.length : 5)});
    $('#messageLabel').sfLabel({text:'Aperte a tecla AZUL para ver recomendações'});
    $('#blackListMessage').sfLabel({text:'Aperte a tecla VERDE para rejeitar a sugestão selecionada nas próximas vezes'});
    $('#clock').sfImage({src: '/images/clock.png'});

    $('#genreLabel').hide();
    $('#genreButton').hide();
    $('#recommendations').hide();
    $('#clock').hide();
    $('#remminder').hide();
    $('#blackListMessage').hide();
}

SceneMainScene.prototype.handleShow = function () {
    var presentProgramDuration = parseInt(tvObject.GetPresentProgram_Duration());
    var presentProgramDuration = 800;
    var factor = 0.8;
    var self = this;
    
    if (this.recommender.appConfig['display-time'] == 1) {
        window.setTimeout(function() {
            $('#messageLabel').hide();
            $('#blackListMessage').show();
            self.recommendations = self.recommender.getRecommendations();
            self.recommender.sendRequests();
            $('#recommendations').sfList({data:self.recommendations.programs[self.currentGenreIndex], index:'0'});
            $('#recommendations').show();
            $('#genreLabel').show();
            $('#genreButton').show();
        }, presentProgramDuration / 2);
    } else if (this.recommender.appConfig['display-time'] == 2) {
        window.setTimeout(function() {
            $('#messageLabel').hide();
            $('#blackListMessage').show();
            self.recommendations = self.recommender.getRecommendations();
            self.recommender.sendRequests();
            $('#recommendations').sfList({data:self.recommendations.programs[self.currentGenreIndex], index:'0'});
            $('#recommendations').show();
            $('#genreLabel').show();
            $('#genreButton').show();
        }, presentProgramDuration);
    }

    if ((this.recommender.appConfig['display-time'] == 1) ||
        (this.recommender.appConfig['display-time'] == 2)) {
        window.setTimeout(function() { $('#messageLabel').hide(); }, 3000);
        window.setTimeout(function() { $('#messageLabel').show(); }, presentProgramDuration*factor*10);
    }
}

SceneMainScene.prototype.handleHide = function () {
	alert("SceneMainScene.handleHide()");
}

SceneMainScene.prototype.handleFocus = function () {
	alert("SceneMainScene.handleFocus()");
}

SceneMainScene.prototype.handleBlur = function () {
	alert("SceneMainScene.handleBlur()");
}

SceneMainScene.prototype.handleKeyDown = function (keyCode) {
	alert("SceneMainScene.handleKeyDown(" + keyCode + ")");
	switch (parseInt(keyCode)) {
		case $.sfKey.LEFT:
            if (document.getElementById('recommendations').style.display == 'none')
                break;
            if (this.currentGenreIndex != 0)
                this.currentGenreIndex--;
            this.currentProgramIndex = 0;
            
            if (this.recommender.canSchedule(this.recommendations.programs[this.currentGenreIndex][0]))
                $('#clock').show();
            else
                $('#clock').hide();
            $('#genreLabel').sfLabel({ text: this.recommendations.genres[this.currentGenreIndex] });
            $('#recommendations').sfList({data:this.recommendations.programs[this.currentGenreIndex], index:'0'});
			break;
		case $.sfKey.RIGHT:
            if (document.getElementById('recommendations').style.display == 'none')
                break;
            if (this.currentGenreIndex != this.recommendations.genres.length-1)
                this.currentGenreIndex++;
            this.currentProgramIndex = 0;
            
            if (this.recommender.canSchedule(this.recommendations.programs[this.currentGenreIndex][0]))
                $('#clock').show();
            else
                $('#clock').hide();
            $('#genreLabel').sfLabel({ text: this.recommendations.genres[this.currentGenreIndex] });
            $('#recommendations').sfList({data:this.recommendations.programs[this.currentGenreIndex], index:'0'});
			break;
		case $.sfKey.UP:
            if (document.getElementById('recommendations').style.display != 'none' &&
                this.currentProgramIndex > 0) {
                var selectedProgram = this.recommendations.programs[this.currentGenreIndex][--this.currentProgramIndex];
                
                if (this.recommender.canSchedule(selectedProgram))
                    $('#clock').show();
                else
                    $('#clock').hide();
                $('#recommendations').sfList('prev');
            }
			break;
		case $.sfKey.DOWN:
            if (document.getElementById('recommendations').style.display != 'none' &&
                this.recommendations.programs[this.currentGenreIndex].length-1 > this.currentProgramIndex) {
                var selectedProgram = this.recommendations.programs[this.currentGenreIndex][++this.currentProgramIndex];
                
                if (this.recommender.canSchedule(selectedProgram))
                    $('#clock').show();
                else
                    $('#clock').hide();
                $('#recommendations').sfList('next');
            }
			break;
        case $.sfKey.RED:
            if (document.getElementById('recommendations').style.display == 'none')
                break;
            $('#recommendations').hide();
            $('#genreLabel').hide();
            $('#genreButton').hide();
            $('#blackListMessage').hide();
            $('#clock').hide();
            this.currentGenreIndex = 0;
            this.currentProgramIndex = 0;
            break;
        case $.sfKey.BLUE:
            $('#messageLabel').hide();
            $('#clock').hide();
            this.recommendations = this.recommender.getRecommendations();
            this.recommender.sendRequests();
            $('#recommendations').sfList({data:this.recommendations.programs[this.currentGenreIndex], index:'0'});
            $('#recommendations').show();
            $('#blackListMessage').show();
            window.setTimeout(function() { $('#blackListMessage').hide(); }, 3000);
            $('#genreLabel').show();
            $('#genreButton').show();
            this.currentProgramIndex = 0;
            break;
        case $.sfKey.YELLOW:
            var scheduledProgram = this.recommender.getScheduledProgram(this.recommendations.programs[this.currentGenreIndex][this.currentProgramIndex]);
            
            if (scheduledProgram != undefined) {
                window.setTimeout(function() {
                    $('#remminder').sfLabel({text:'O programa ' + scheduledProgram.program + ' iniciará em 5 minutos no canal ' + scheduledProgram.channel});
                    $('#remminder').show();
                }, scheduledProgram.startTime);
            }
            break;
        case $.sfKey.GREEN:
            break;
		case $.sfKey.ENTER:
            alert("Program " + this.recommendations.programs[this.currentGenreIndex][this.currentProgramIndex] + " selected.");
			break;
        case 68:
        case 65:
            this.recommender.channelChanged();
            if (this.recommender.appConfig['display-time'] == 0) {
                $('#messageLabel').hide();
                $('#clock').hide();
                this.recommendations = this.recommender.getRecommendations();
                this.recommender.sendRequests();
                $('#recommendations').sfList({data:this.recommendations.programs[this.currentGenreIndex], index:'0'});
                $('#recommendations').show();
                $('#genreLabel').show();
                $('#genreButton').show();
            }
            break;
        case 75:
            $.sfScene.hide('MainScene');
            $.sfScene.show('ConfigScene');
            $.sfScene.focus('ConfigScene');
            break;
	}
}